﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKRevitBase.WinForm;
using YJKOpeningCommon.DataEnum;
using YJKOpeningCommon.Public;
using YJKOpeningDesignInput.ConfigFormData;

namespace YJKOpeningDesignInput.ConfigForm
{
	public partial class CasingPipeAddForm : ConfigDataBaseForm
	{
        public CasingPipeAddForm(WindowHandle wndRevit)
		{
			this.m_hWndRevit = wndRevit;
			this.InitializeComponent();
			base.SetFormLocation(this.strXmlPath);
			this.m_xml = new XmlDocument();
			this.InitByDefaultData();
			if (File.Exists(this.strCasingPipeFormConfigPath))
			{
				this.ReadHistoryData();
			}
			this.m_bSelPipeTypeChange = false;
			this.SetFormData();
			this.ChangePipeDiaType();
		}

		private void InitByDefaultData()
		{
			this.m_CpdcSteel = new CasingPipeDataConfig();
			this.m_CpdcSteel.m_strCasingPipeName = "穿墙套管";
			this.m_CpdcSteel.m_lstPipeDiaType.Add("DN");
			this.m_CpdcSteel.m_strDefaultPipeDiaType = "DN";
			this.m_CpdcWaterProofFlexibleA = new CasingPipeDataConfig();
			this.m_CpdcWaterProofFlexibleA.m_strCasingPipeName = "柔性防水套管A";
			this.m_CpdcWaterProofFlexibleA.m_lstPipeDiaType.Add("D1");
			this.m_CpdcWaterProofFlexibleA.m_lstPipeDiaType.Add("D2");
			this.m_CpdcWaterProofFlexibleA.m_lstPipeDiaType.Add("D3");
			this.m_CpdcWaterProofFlexibleA.m_lstPipeDiaType.Add("D4");
			this.m_CpdcWaterProofFlexibleA.m_lstPipeDiaType.Add("D5");
			this.m_CpdcWaterProofFlexibleA.m_strDefaultPipeDiaType = "D3";
			this.m_CpdcWaterProofFlexibleB = new CasingPipeDataConfig();
			this.m_CpdcWaterProofFlexibleB.m_strCasingPipeName = "柔性防水套管B";
			this.m_CpdcWaterProofFlexibleB.m_lstPipeDiaType.Add("D1");
			this.m_CpdcWaterProofFlexibleB.m_lstPipeDiaType.Add("D2");
			this.m_CpdcWaterProofFlexibleB.m_lstPipeDiaType.Add("D3");
			this.m_CpdcWaterProofFlexibleB.m_lstPipeDiaType.Add("D4");
			this.m_CpdcWaterProofFlexibleB.m_lstPipeDiaType.Add("D5");
			this.m_CpdcWaterProofFlexibleB.m_strDefaultPipeDiaType = "D3";
			this.m_WaterProofInflexibleA = new CasingPipeDataConfig();
			this.m_WaterProofInflexibleA.m_strCasingPipeName = "刚性防水套管A";
			this.m_WaterProofInflexibleA.m_lstPipeDiaType.Add("D1");
			this.m_WaterProofInflexibleA.m_lstPipeDiaType.Add("D2");
			this.m_WaterProofInflexibleA.m_lstPipeDiaType.Add("D3");
			this.m_WaterProofInflexibleA.m_lstPipeDiaType.Add("D4");
			this.m_WaterProofInflexibleA.m_strDefaultPipeDiaType = "D3";
			this.m_WaterProofInflexibleB = new CasingPipeDataConfig();
			this.m_WaterProofInflexibleB.m_strCasingPipeName = "刚性防水套管B";
			this.m_WaterProofInflexibleB.m_lstPipeDiaType.Add("D1");
			this.m_WaterProofInflexibleB.m_lstPipeDiaType.Add("D2");
			this.m_WaterProofInflexibleB.m_lstPipeDiaType.Add("D3");
			this.m_WaterProofInflexibleB.m_strDefaultPipeDiaType = "D3";
			this.m_WaterProofInflexibleC = new CasingPipeDataConfig();
			this.m_WaterProofInflexibleC.m_strCasingPipeName = "刚性防水套管C";
			this.m_WaterProofInflexibleC.m_lstPipeDiaType.Add("D1");
			this.m_WaterProofInflexibleC.m_lstPipeDiaType.Add("D2");
			this.m_WaterProofInflexibleC.m_lstPipeDiaType.Add("D3");
			this.m_WaterProofInflexibleC.m_lstPipeDiaType.Add("D4");
			this.m_WaterProofInflexibleC.m_strDefaultPipeDiaType = "D3";
			this.m_radAirtightFlexible = new CasingPipeDataConfig();
			this.m_radAirtightFlexible.m_strCasingPipeName = "柔性密闭套管";
			this.m_radAirtightFlexible.m_lstPipeDiaType.Add("D1");
			this.m_radAirtightFlexible.m_lstPipeDiaType.Add("D2");
			this.m_radAirtightFlexible.m_lstPipeDiaType.Add("D3");
			this.m_radAirtightFlexible.m_lstPipeDiaType.Add("D4");
			this.m_radAirtightFlexible.m_lstPipeDiaType.Add("D5");
			this.m_radAirtightFlexible.m_lstPipeDiaType.Add("D6");
			this.m_radAirtightFlexible.m_lstPipeDiaType.Add("D7");
			this.m_radAirtightFlexible.m_strDefaultPipeDiaType = "D3";
			this.m_radAirtightInflexible = new CasingPipeDataConfig();
			this.m_radAirtightInflexible.m_strCasingPipeName = "刚性密闭套管";
			this.m_radAirtightInflexible.m_lstPipeDiaType.Add("D1");
			this.m_radAirtightInflexible.m_lstPipeDiaType.Add("D2");
			this.m_radAirtightInflexible.m_lstPipeDiaType.Add("D3");
			this.m_radAirtightInflexible.m_lstPipeDiaType.Add("D4");
			this.m_radAirtightInflexible.m_strDefaultPipeDiaType = "D3";
			this.m_nDrivepipeType = ECasingPipeType.eSteel;
			this.m_nSelPipeType = 0;
		}

		private void SetFormData()
		{
			this.radSteel.Checked = (this.m_nDrivepipeType == ECasingPipeType.eSteel);
			this.radWaterProofFlexibleA.Checked = object.Equals(this.m_nDrivepipeType, ECasingPipeType.eWaterProofFlexibleA);
			this.radWaterProofFlexibleB.Checked = object.Equals(this.m_nDrivepipeType, ECasingPipeType.eWaterProofFlexibleB);
			this.radWaterProofInflexibleA.Checked = object.Equals(this.m_nDrivepipeType, ECasingPipeType.eWaterProofInflexibleA);
			this.radWaterProofInflexibleB.Checked = object.Equals(this.m_nDrivepipeType, ECasingPipeType.eWaterProofInflexibleB);
			this.radWaterProofInflexibleC.Checked = object.Equals(this.m_nDrivepipeType, ECasingPipeType.eWaterProofInflexibleC);
			this.radAirtightFlexible.Checked = object.Equals(this.m_nDrivepipeType, ECasingPipeType.eAirtightFlexible);
			this.radAirtightInflexible.Checked = object.Equals(this.m_nDrivepipeType, ECasingPipeType.eAirtightInflexible);
			this.radSingle.Checked = object.Equals(this.m_nSelPipeType, 0);
			this.radMulti.Checked = object.Equals(this.m_nSelPipeType, 1);
		}

		private bool ReadHistoryData()
		{
			try
			{
				this.m_xml.Load(this.strCasingPipeFormConfigPath);
				XmlNodeList elementsByTagName = this.m_xml.GetElementsByTagName("套管类别");
				this.m_nDrivepipeType = (ECasingPipeType)Convert.ToInt32(elementsByTagName[0].Attributes["默认"].InnerText.ToString());
				elementsByTagName = this.m_xml.GetElementsByTagName("水管选择方式");
				this.m_nSelPipeType = Convert.ToInt32(elementsByTagName[0].Attributes["默认"].InnerText.ToString());
				this.ReadCasingPipeData(this.m_CpdcSteel);
				this.ReadCasingPipeData(this.m_CpdcWaterProofFlexibleA);
				this.ReadCasingPipeData(this.m_CpdcWaterProofFlexibleB);
				this.ReadCasingPipeData(this.m_WaterProofInflexibleA);
				this.ReadCasingPipeData(this.m_WaterProofInflexibleB);
				this.ReadCasingPipeData(this.m_WaterProofInflexibleC);
				this.ReadCasingPipeData(this.m_radAirtightFlexible);
				this.ReadCasingPipeData(this.m_radAirtightInflexible);
			}
			catch
			{
				return false;
			}
			return true;
		}

		private void ReadCasingPipeData(CasingPipeDataConfig casingPipeDataConfig)
		{
			try
			{
				XmlNodeList elementsByTagName = this.m_xml.GetElementsByTagName(casingPipeDataConfig.m_strCasingPipeName);
				casingPipeDataConfig.m_strDefaultPipeDiaType = elementsByTagName[0].Attributes["默认"].InnerText.ToString();
				casingPipeDataConfig.SetPipeDiaTypeString(elementsByTagName[0].Attributes["管径类别"].InnerText.ToString());
			}
			catch
			{
			}
		}

		private void WriteCasingPipeData(CasingPipeDataConfig casingPipeDataConfig, bool bIsExist)
		{
			try
			{
				if (bIsExist)
				{
					XmlNodeList elementsByTagName = this.m_xml.GetElementsByTagName(casingPipeDataConfig.m_strCasingPipeName);
					if (elementsByTagName.Count > 0)
					{
						elementsByTagName[0].Attributes["管径类别"].InnerText = casingPipeDataConfig.GetPipeDiaTypeString();
						elementsByTagName[0].Attributes["默认"].InnerText = casingPipeDataConfig.m_strDefaultPipeDiaType;
					}
					else
					{
						elementsByTagName = this.m_xml.GetElementsByTagName("套管对话框属性值");
						XmlElement xmlElement = this.m_xml.CreateElement(casingPipeDataConfig.m_strCasingPipeName);
						xmlElement.SetAttribute("管径类别", casingPipeDataConfig.GetPipeDiaTypeString());
						xmlElement.SetAttribute("默认", casingPipeDataConfig.m_strDefaultPipeDiaType);
						elementsByTagName[0].AppendChild(xmlElement);
					}
				}
				else
				{
					XmlNodeList elementsByTagName2 = this.m_xml.GetElementsByTagName("套管对话框属性值");
					XmlElement xmlElement2 = this.m_xml.CreateElement(casingPipeDataConfig.m_strCasingPipeName);
					xmlElement2.SetAttribute("管径类别", casingPipeDataConfig.GetPipeDiaTypeString());
					xmlElement2.SetAttribute("默认", casingPipeDataConfig.m_strDefaultPipeDiaType);
					elementsByTagName2[0].AppendChild(xmlElement2);
				}
			}
			catch
			{
			}
		}

		private void WriteHistoryData()
		{
			try
			{
				if (File.Exists(this.strCasingPipeFormConfigPath))
				{
					XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
					xmlReaderSettings.IgnoreComments = true;
					XmlReader xmlReader = XmlReader.Create(this.strCasingPipeFormConfigPath, xmlReaderSettings);
					this.m_xml.Load(xmlReader);
					this.m_xml.GetElementsByTagName("水管选择方式")[0].Attributes["默认"].InnerText = this.m_nSelPipeType.ToString();
					XmlNode xmlNode = this.m_xml.GetElementsByTagName("套管类别")[0].Attributes["默认"];
					int nDrivepipeType = (int)this.m_nDrivepipeType;
					xmlNode.InnerText = nDrivepipeType.ToString();
					this.WriteCasingPipeData(this.m_CpdcSteel, true);
					this.WriteCasingPipeData(this.m_CpdcWaterProofFlexibleA, true);
					this.WriteCasingPipeData(this.m_CpdcWaterProofFlexibleB, true);
					this.WriteCasingPipeData(this.m_WaterProofInflexibleA, true);
					this.WriteCasingPipeData(this.m_WaterProofInflexibleB, true);
					this.WriteCasingPipeData(this.m_WaterProofInflexibleC, true);
					this.WriteCasingPipeData(this.m_radAirtightFlexible, true);
					this.WriteCasingPipeData(this.m_radAirtightInflexible, true);
					xmlReader.Close();
				}
				else
				{
					XmlDeclaration newChild = this.m_xml.CreateXmlDeclaration("1.0", "gb2312", null);
					this.m_xml.AppendChild(newChild);
					XmlNode xmlNode2 = this.m_xml.CreateElement("套管对话框属性值");
					this.m_xml.AppendChild(xmlNode2);
					XmlElement xmlElement = this.m_xml.CreateElement("套管类别");
					XmlElement xmlElement2 = xmlElement;
					string name = "默认";
					int nDrivepipeType = (int)this.m_nDrivepipeType;
					xmlElement2.SetAttribute(name, nDrivepipeType.ToString());
					xmlNode2.AppendChild(xmlElement);
					xmlElement = this.m_xml.CreateElement("水管选择方式");
					xmlElement.SetAttribute("默认", this.m_nSelPipeType.ToString());
					xmlNode2.AppendChild(xmlElement);
					this.WriteCasingPipeData(this.m_CpdcSteel, false);
					this.WriteCasingPipeData(this.m_CpdcWaterProofFlexibleA, false);
					this.WriteCasingPipeData(this.m_CpdcWaterProofFlexibleB, false);
					this.WriteCasingPipeData(this.m_WaterProofInflexibleA, false);
					this.WriteCasingPipeData(this.m_WaterProofInflexibleB, false);
					this.WriteCasingPipeData(this.m_WaterProofInflexibleC, false);
					this.WriteCasingPipeData(this.m_radAirtightFlexible, false);
					this.WriteCasingPipeData(this.m_radAirtightInflexible, false);
				}
				this.m_xml.Save(this.strCasingPipeFormConfigPath);
			}
			catch (Exception)
			{
			}
		}

		private CasingPipeDataConfig GetCasingPipeDataConfig()
		{
			CasingPipeDataConfig result;
			if (this.m_nDrivepipeType == ECasingPipeType.eSteel)
			{
				result = this.m_CpdcSteel;
			}
			else if (this.m_nDrivepipeType == ECasingPipeType.eWaterProofFlexibleA)
			{
				result = this.m_CpdcWaterProofFlexibleA;
			}
			else if (this.m_nDrivepipeType == ECasingPipeType.eWaterProofFlexibleB)
			{
				result = this.m_CpdcWaterProofFlexibleB;
			}
			else if (this.m_nDrivepipeType == ECasingPipeType.eWaterProofInflexibleA)
			{
				result = this.m_WaterProofInflexibleA;
			}
			else if (this.m_nDrivepipeType == ECasingPipeType.eWaterProofInflexibleB)
			{
				result = this.m_WaterProofInflexibleB;
			}
			else if (this.m_nDrivepipeType == ECasingPipeType.eWaterProofInflexibleC)
			{
				result = this.m_WaterProofInflexibleC;
			}
			else if (this.m_nDrivepipeType == ECasingPipeType.eAirtightFlexible)
			{
				result = this.m_radAirtightFlexible;
			}
			else if (this.m_nDrivepipeType == ECasingPipeType.eAirtightInflexible)
			{
				result = this.m_radAirtightInflexible;
			}
			else
			{
				result = null;
			}
			return result;
		}

		private void ChangePipeDiaType()
		{
			this.cmbPipeDiaType.Items.Clear();
			CasingPipeDataConfig casingPipeDataConfig = this.GetCasingPipeDataConfig();
			foreach (string item in casingPipeDataConfig.m_lstPipeDiaType)
			{
				this.cmbPipeDiaType.Items.Add(item);
			}
			this.cmbPipeDiaType.Text = casingPipeDataConfig.m_strDefaultPipeDiaType;
			this.m_strPipeDiaType = casingPipeDataConfig.m_strDefaultPipeDiaType;
		}

		private void radSteel_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = ECasingPipeType.eSteel;
			this.ChangePipeDiaType();
		}

		private void radWaterProofFlexibleA_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = ECasingPipeType.eWaterProofFlexibleA;
			this.ChangePipeDiaType();
		}

		private void radWaterProofFlexibleB_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = ECasingPipeType.eWaterProofFlexibleB;
			this.ChangePipeDiaType();
		}

		private void radWaterProofInflexibleA_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = ECasingPipeType.eWaterProofInflexibleA;
			this.ChangePipeDiaType();
		}

		private void radWaterProofInflexibleB_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = ECasingPipeType.eWaterProofInflexibleB;
			this.ChangePipeDiaType();
		}

		private void radWaterProofInflexibleC_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = ECasingPipeType.eWaterProofInflexibleC;
			this.ChangePipeDiaType();
		}

		private void radAirtightFlexible_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = ECasingPipeType.eAirtightFlexible;
			this.ChangePipeDiaType();
		}

		private void radAirtightInflexible_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = ECasingPipeType.eAirtightInflexible;
			this.ChangePipeDiaType();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void radSingle_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_nSelPipeType != 0)
			{
				this.m_nSelPipeType = 0;
				this.m_bSelPipeTypeChange = true;
				this.CancelCommand();
			}
		}

		private void radMulti_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_nSelPipeType != 1)
			{
				this.m_nSelPipeType = 1;
				this.m_bSelPipeTypeChange = true;
				this.CancelCommand();
			}
		}

		private void CasingPipeAddForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			try
			{
				if (e.KeyChar == '\u001b')
				{
					base.Close();
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		private void CancelCommand()
		{
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			CasingPipeAddForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			CasingPipeAddForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			CasingPipeAddForm.SendMessage(this.m_hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void CasingPipeAddForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				if (File.Exists(this.strXmlPath))
				{
					XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
					xmlReaderSettings.IgnoreComments = true;
					XmlReader xmlReader = XmlReader.Create(this.strXmlPath, xmlReaderSettings);
					xmlDocument.Load(xmlReader);
					XmlElement documentElement = xmlDocument.DocumentElement;
					documentElement.SetAttribute("LocationX", base.Location.X.ToString());
					documentElement.SetAttribute("LocationY", base.Location.Y.ToString());
					xmlReader.Close();
				}
				else
				{
					XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
					xmlDocument.AppendChild(newChild);
					XmlElement xmlElement = xmlDocument.CreateElement("HistoryData");
					xmlDocument.AppendChild(xmlElement);
					xmlElement.SetAttribute("LocationX", base.Location.X.ToString());
					xmlElement.SetAttribute("LocationY", base.Location.Y.ToString());
				}
				xmlDocument.Save(this.strXmlPath);
			}
			catch (Exception)
			{
				throw;
			}
			this.WriteHistoryData();
			this.CancelCommand();
		}

		private void cmbPipeDiaType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.GetCasingPipeDataConfig().m_strDefaultPipeDiaType = this.cmbPipeDiaType.Text;
			this.m_strPipeDiaType = this.cmbPipeDiaType.Text;
		}

		public ECasingPipeType m_nDrivepipeType;

		public int m_nSelPipeType;

		public bool m_bSelPipeTypeChange;

		public string m_strPipeDiaType;

		private XmlDocument m_xml;

		private CasingPipeDataConfig m_CpdcSteel;

		private CasingPipeDataConfig m_CpdcWaterProofFlexibleA;

		private CasingPipeDataConfig m_CpdcWaterProofFlexibleB;

		private CasingPipeDataConfig m_WaterProofInflexibleA;

		private CasingPipeDataConfig m_WaterProofInflexibleB;

		private CasingPipeDataConfig m_WaterProofInflexibleC;

		private CasingPipeDataConfig m_radAirtightFlexible;

		private CasingPipeDataConfig m_radAirtightInflexible;

        private WindowHandle m_hWndRevit;

		private string strXmlPath = Product.UserDataLocation + "\\CasingPipeAddForm.xml";

		private string strCasingPipeFormConfigPath = Product.UserDataLocation + "\\CasingPipeFormConfig.xml";
	}
}
